package fuza.android.thetrafficsigns.help;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import fuza.android.thetrafficsigns.R;
import fuza.android.thetrafficsigns.Rambu;

public class ListViewAdapter extends BaseAdapter {

	// Declare Variables
	Context mContext;
	LayoutInflater inflater;
	private List<Rambu> listed = null;
	private ArrayList<Rambu> arraylist;
	
	ImageView img;

	public ListViewAdapter(Context context, List<Rambu> dataList) {
		mContext = context;
		this.listed = dataList;
		inflater = LayoutInflater.from(mContext);
		this.arraylist = new ArrayList<Rambu>();
		this.arraylist.addAll(dataList);
	}

	public class ViewHolder {
		TextView country;
		TextView population;
		ImageView img;
	}

	@Override
	public int getCount() {
		return listed.size();
	}

	@Override
	public Rambu getItem(int position) {
		return listed.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public View getView(final int position, View view, ViewGroup parent) {
		final ViewHolder holder;
		if (view == null) {
			holder = new ViewHolder();
			view = inflater.inflate(R.layout.listview_item, null);
			
			// Locate the TextViews in listview_item.xml
			holder.img = (ImageView) view.findViewById(R.id.imageK);
			holder.country = (TextView) view.findViewById(R.id.country);
			holder.population = (TextView) view.findViewById(R.id.population);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		// Set the results into TextViews
		holder.img.setImageResource(mContext.getResources().getIdentifier(listed.get(position).getN_gambar().replace(".png", ""), "drawable", "fuza.android.thetrafficsigns"));
		holder.country.setText(listed.get(position).getDesc_rambu());
		holder.population.setText(listed.get(position).getJenis_rambu());

		// Listen for ListView Item Click
		view.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// Send single item click data to SingleItemView Class
				Intent intent = new Intent(mContext, SingleItemView.class);
				// Pass all data rank
				intent.putExtra("id", listed.get(position).getId()+"");
				// Pass all data country
				// Start SingleItemView Class
				mContext.startActivity(intent);
			}
		});

		return view;
	}

	// Filter Class
	public void filter(String charText) {
		charText = charText.toLowerCase(Locale.getDefault());
		listed.clear();
		if (charText.length() == 0) {
			listed.addAll(arraylist);
		} else {
			for (Rambu wp : arraylist) {
				if (wp.getDesc_rambu().toLowerCase(Locale.getDefault())
						.contains(charText)) {
					listed.add(wp);
				}
			}
		}
		notifyDataSetChanged();
	}

}